package com.Biomedica.virtualpowerpoint;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import android.os.Handler;
import android.util.Log;

public class Reciver {
	String Ip;
    int Port;
    
   DatagramSocket s;
   DatagramPacket p;
   InetAddress address;

   byte[] buffer=new byte[255];
   
   PacketRecived OnPacketRecived;
   public void setOnPacketRecived(PacketRecived p)
   {
	   OnPacketRecived=p;
   }
   public Reciver()
   {
	   
   }
   
   public void Start(int port) throws SocketException
   {
	   try {
		Port=port;
		   s=new DatagramSocket(port);
		
		  
		  new Thread(new ServerRunnable()).start();
		  
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }
   // The main server loop
   private class ServerRunnable implements Runnable
   {
	   

	public void run() {
		// TODO Auto-generated method stub
		
		p=new DatagramPacket(buffer,buffer.length);
		
		try {
			while(true)
			{
			s.receive(p);
			String ss=new String(p.getData());
			InterpretePacket(ss);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	   
   }
   private void InterpretePacket(String code)
   {
	   OnPacketRecived.OnPacketRecived(code);
   }
   public interface PacketRecived
   {
	   void OnPacketRecived(String Msg);
   }
}
